<?php

namespace app\api\validate;

use think\Db;
use think\Validate;

/**
 * Class CarValidate
 * @package app\api\validate
 * @author zhenHong~
 * api/Car验证类
 */
class CarValidate extends Validate
{
    protected $rule = [
        'b_id' => ['require', 'number'],
        't_id' => ['require', 'number'],
        'm_id' => ['require', 'number'],
        'pageNum' => ['require', 'number'],
        'listNum' => ['require', 'number'],
        'start_time' => ['checkMyTime'],
        'end_time' => ['checkMyTime'],
        'u_id' => ['require', 'number'],
        'b_name' => ['require'],
        't_name' => ['require'],
        'm_name' => ['require'],
        'licenseplate' => ['require', 'checkCarCode'],
        'c_id' => ['require', 'number']
    ];

    protected $message = [
        'licenseplate.require' => '车牌照不能为空',
        'licenseplate.checkCarCode' => '车牌号已存在请重新输入车牌号',
        'b_id' => '车辆品牌id参数错误',
        't_id' => '车辆系别id参数错误',
        'm_id' => '车辆车型id参数错误',
        'pageNum' => '分页参数错误',
        'listNum' => '查询条数参数错误',
        'start_time' => '开始时间参数错误',
        'end_time' => '结束时间参数错误',
        'u_id' => '用户参数错误',
        'b_name' => '品牌名不能为空',
        'm_name' => '车型名不能为空',
        't_name' => '车系名不能为空',
        'c_id' => '车辆id参数错误'
    ];

    protected $scene = [
        //用户添加车辆
        'addCar' => [
            'u_id', 'b_id', 't_id', 'm_id', 'b_name', 't_name', 'm_name',
            'licenseplate',
        ],
        //用户查询车辆列表信息
        'carsFind' => ['u_id'],
        //用户编辑车辆
        'carEdit' => ['c_id','u_id','b_id','t_id','m_id','b_name','t_name','m_name'],
        //设置默认车辆
        'carDefault' => ['u_id', 'c_id'],
        //用户查询车辆详情
        'carInfo' => ['c_id'],
        'carDel' => ['c_id']
    ];

    /**
     * @param $value
     * @return bool
     * 检查时间参数
     */
    protected function checkMyTime($value)
    {
        if (date('Y-m-d H:i:s', strtotime($value)) != $value) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param $value
     * @return bool
     * 判断是否已经存在该车牌号
     * @author zhenHong~
     */
    protected function checkCarCode($value, $rule = '', $data)
    {
        //这里要考虑到编辑车辆的情况
        if(array_key_exists('c_id',$data)){
            $where = [
                'licenseplate' => $value,
                'status' => 0,
                'c_id' => ['neq', $data['c_id']]
            ];
        } else {
            $where = [
                'licenseplate' => $value,
                'status' => 0
            ];
        }
        $carCode = Db::name('Carinfor')
            ->where($where)
            ->value('licenseplate');
        return $carCode ? false : true;
    }

    /**
     * @param $value
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 判断obd设备号
     * @author zhenHong~
     */
    protected function checkMyDevice($value)
    {
        $bool = true;
        //先查询设备表 看该设备号是否存在且为可用状态
        $device = Db::name('Device')
            ->where('id', $value)
            ->field('id,status,org_id,car_id')
            ->find();
        if ($device && $device['org_id'] != 0 && $device['status'] == 2 && $device['car_id'] == 0) {
            $bool = true;
        } else {
            $bool = false;
        }
        //再查询车辆信息表 看该设备是否已经被绑定且该车为正常状态
        $carInfo = Db::name('Carinfor')
            ->field('c_id')
            ->where([
                'o_id' => $value,
                'status' => 0
            ])->find();
        if ($carInfo) $bool = false;
        return $bool ? true : false;
    }
}